const { resolve } = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin');

module.exports = {

    mode: "development",

    entry: {
        base: './src/base.js',
        login: './src/pages/login/index.js',
        users: './src/pages/users/index.js',
        userAdd: './src/pages/userAdd/index.js',
        userUpdate: './src/pages/userUpdate/index.js'
    },

    output: {
        path: resolve(__dirname, 'dist'),
        filename: "./js/[name].[hash:8].js",
        clean: true
    },

    plugins: [
        new HtmlWebpackPlugin({
            template: './src/pages/login/index.html',
            filename: "login.html",
            chunks: ['base', 'login'],
            inject: 'body'
        }),
        new HtmlWebpackPlugin({
            template: './src/pages/users/index.html',
            filename: "users.html",
            chunks: ['base', 'users'],
            inject: 'body'
        }),
        new HtmlWebpackPlugin({
            template: './src/pages/userAdd/index.html',
            filename: "userAdd.html",
            chunks: ['base', 'userAdd'],
            inject: 'body'
        }),
        new HtmlWebpackPlugin({
            template: './src/pages/userUpdate/index.html',
            filename: "userUpdate.html",
            chunks: ['base', 'userUpdate'],
            inject: 'body'
        }),
        new HtmlWebpackPlugin({
            template: './src/pages/index/index.html',
            filename: "index.html",
        })
    ],

    devServer: {
        port: 3001,
        open: true,
        proxy: {
            '/api': {
                    target: "http://localhost:8081",
                    pathRewrite: {
                        "^/api": ""
                    }
                }
            }
        },

        module: {
            rules: [
                // 处理JS文件 ES6 => ES5
                {
                    test: /\.js$/,
                    exclude: /node_modules/, // 不进行处理的文件夹
                    use: {
                        loader: 'babel-loader',
                        options: {
                            presets: [
                                [
                                    '@babel/preset-env',
                                    {
                                        useBuiltIns: 'usage', // 只打包使用的ES6新API的实现代码
                                        corejs: { version: 2 } // 指定core-js的版本号为2
                                    }
                                ]
                            ]
                        }
                    }
                },

                // 处理css文件
                {
                    test: /\.css$/,  // 配置css文件的正则
                    use: [
                        'style-loader', // js文件 => 页面的style标签
                        'css-loader'  // css文件 => js文件
                    ]
                },

                // 处理art文件
                {
                    test: /\.art$/,
                    loader: "art-template-loader",
                },
            ]
        },

        devtool: 'cheap-module-source-map',

    }